#include <iostream>
#include <chrono>
#include <vector>
#include "../include/allocator.h"

// 测试类
class TestClass {
private:
    int data[10];
public:
    TestClass() {
        for (int i = 0; i < 10; ++i) {
            data[i] = i;
        }
    }

    void print() {
        std::cout << "TestClass data: ";
        for (int i = 0; i < 10; ++i) {
            std::cout << data[i] << " ";
        }
        std::cout << std::endl;
    }
};

// 性能测试函数
void performance_test() {
    const int TEST_SIZE = 100000;
    
    // 使用标准分配器
    auto start1 = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < TEST_SIZE; ++i) {
        int* p = new int;
        *p = i;
        delete p;
    }
    auto end1 = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed1 = end1 - start1;
    
    // 使用内存池分配器
    auto start2 = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < TEST_SIZE; ++i) {
        int* p = sgi_memo_pool::TypedAllocator<int>::allocate();
        *p = i;
        sgi_memo_pool::TypedAllocator<int>::deallocate(p);
    }
    auto end2 = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed2 = end2 - start2;
    
    std::cout << "Standard allocator time: " << elapsed1.count() << " seconds" << std::endl;
    std::cout << "Memory pool allocator time: " << elapsed2.count() << " seconds" << std::endl;
    std::cout << "Performance improvement: " << (elapsed1.count() / elapsed2.count()) << "x" << std::endl;
}

// 功能测试函数
void functionality_test() {
    std::cout << "\n=== 功能测试 ===" << std::endl;
    
    // 分配单个对象
    std::cout << "分配单个对象:" << std::endl;
    TestClass* obj = sgi_memo_pool::TypedAllocator<TestClass>::allocate();
    sgi_memo_pool::TypedAllocator<TestClass>::construct(obj, TestClass());
    obj->print();
    sgi_memo_pool::TypedAllocator<TestClass>::destroy(obj);
    sgi_memo_pool::TypedAllocator<TestClass>::deallocate(obj);
    
    // 分配数组
    std::cout << "\n分配数组:" << std::endl;
    const int ARRAY_SIZE = 5;
    int* array = sgi_memo_pool::TypedAllocator<int>::allocate(ARRAY_SIZE);
    for (int i = 0; i < ARRAY_SIZE; ++i) {
        array[i] = i * 10;
    }
    
    for (int i = 0; i < ARRAY_SIZE; ++i) {
        std::cout << "array[" << i << "] = " << array[i] << std::endl;
    }
    
    sgi_memo_pool::TypedAllocator<int>::deallocate(array, ARRAY_SIZE);
    
    // 测试内存重分配
    std::cout << "\n测试内存重分配:" << std::endl;
    int* data = static_cast<int*>(sgi_memo_pool::Allocator::allocate(sizeof(int) * 2));
    data[0] = 100;
    data[1] = 200;
    
    std::cout << "原始数据: " << data[0] << ", " << data[1] << std::endl;
    
    data = static_cast<int*>(sgi_memo_pool::Allocator::reallocate(data, sizeof(int) * 2, sizeof(int) * 4));
    data[2] = 300;
    data[3] = 400;
    
    std::cout << "重分配后数据: " << data[0] << ", " << data[1] << ", " << data[2] << ", " << data[3] << std::endl;
    
    sgi_memo_pool::Allocator::deallocate(data, sizeof(int) * 4);
}

// 内存泄漏测试
void memory_leak_test() {
    std::cout << "\n=== 内存泄漏测试 ===" << std::endl;
    
    const int TEST_SIZE = 10000;
    std::vector<int*> pointers;
    
    // 分配大量内存
    for (int i = 0; i < TEST_SIZE; ++i) {
        int* p = sgi_memo_pool::TypedAllocator<int>::allocate();
        *p = i;
        pointers.push_back(p);
    }
    
    std::cout << "分配了 " << TEST_SIZE << " 个对象" << std::endl;
    
    // 释放所有内存
    for (auto p : pointers) {
        sgi_memo_pool::TypedAllocator<int>::deallocate(p);
    }
    
    std::cout << "释放了所有对象" << std::endl;
}

int main() {
    std::cout << "SGI 内存池测试程序" << std::endl;
    std::cout << "===================" << std::endl;
    
    // 运行性能测试
    std::cout << "\n=== 性能测试 ===" << std::endl;
    performance_test();
    
    // 运行功能测试
    functionality_test();
    
    // 运行内存泄漏测试
    memory_leak_test();
    
    return 0;
} 